package com.spring.demo.core.advice;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;

/**
 * 混合 advice demo
 * 
 * @author sean
 * 
 */
public class MixMethodAdvice implements MethodBeforeAdvice,
		AfterReturningAdvice, ThrowsAdvice, MethodInterceptor {

	/**
	 * 在方法之前执行
	 */
	@Override
	public void before(Method method, Object[] args, Object target)
			throws Throwable {
		System.out.println("在方法执行之前执行！" + method.getName());
	}

	/**
	 * 在方法返回之前执行(如果没有抛出异常的话)
	 */
	@Override
	public void afterReturning(Object returnValue, Method method,
			Object[] args, Object target) throws Throwable {
		System.out.println("在方法返回前执行！");
	}

	/**
	 * around advice
	 */
	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		System.out.println("around before!" + invocation.getMethod().getName());
		try {
			Object o = invocation.proceed();
			System.out.println("around after");
			return o;
		} catch (Exception e) {
			System.out.println("around throw a error");
			throw e;
		}
	}

	/**
	 * 在异常抛出之前执行
	 * 
	 * @param e
	 * @throws Throwable
	 */
	public void afterThrowing(IllegalArgumentException e) throws Throwable {
		System.out.println("在异常抛出之前执行");
	}

}
